package com.example.test2.activity;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.test2.R;
import com.example.test2.databinding.ActivityAddCourseBinding;
import com.example.test2.db.control.NewsDbController;
import com.example.test2.db.entity.New;
import com.example.test2.event.NewEvent;
import com.example.test2.http.Constants;
import com.example.test2.utils.FileUtils;
import com.example.test2.utils.ToastUtils;
import com.example.test2.utils.Tools;

import org.greenrobot.eventbus.EventBus;

import java.util.Date;

// TODO: 2024/6/02 添加新闻
public class AddNewsActivity extends BaseActivity<ActivityAddCourseBinding> implements View.OnClickListener {

    @Override
    protected void initData() {
        super.initData();
        getBinding().titleBar.setTitle("发布新闻");
    }

    @Override
    protected void initClick() {
        super.initClick();
        getBinding().titleBar.setLeftIconOnClickListener(v -> {finish();});
        getBinding().btSend.setOnClickListener(this);
        getBinding().ivImg.setOnClickListener(this);
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_add_course;
    }

    @Override
    protected ActivityAddCourseBinding onCreateViewBinding(@NonNull LayoutInflater layoutInflater) {
        return ActivityAddCourseBinding.inflate(layoutInflater);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bt_send:
                send();
                break;
            case R.id.iv_img:
                selectImage();
                break;
        }
    }

    private Uri uri;

    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 1000 && resultCode == Activity.RESULT_OK && null != data) {
            uri = data.getData();
            getBinding().ivImg.setImageURI(uri);
        }
    }

    private void selectImage() {
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        intent.setType("image/*");
        startActivityForResult(intent, 1000);
    }

    private void send() {
        String one = getBinding().etOne.getText().toString();
        String two = getBinding().etTwo.getText().toString();
        String type = getBinding().etType.getText().toString();
        String content = getBinding().etContent.getText().toString();

        String path;
        if (uri != null) {
            path = FileUtils.getPath(this, uri, System.currentTimeMillis() + "");
        } else {
            path = "";
        }
        New mNew = new New();
        mNew.setOneTitle(one);
        mNew.setTwoTitle(two);
        mNew.setDate(Tools.customFormat(new Date(), "yyyy-MM-dd"));
        mNew.setContent(content);
        mNew.setPath(path);
        mNew.setType(type);
        mNew.setAuthor(Constants.PHONE);
        NewsDbController.getInstance(this).insertOrReplace(mNew);
        ToastUtils.getInstance().show(AddNewsActivity.this, "发布成功");
        EventBus.getDefault().post(new NewEvent());
        new Handler().postDelayed(() -> {
            finish();
        }, 500);
    }
}